package com.github.caijh.mall.system.controller;

import java.util.List;
import javax.inject.Inject;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.caijh.commons.util.Strings;
import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.mall.category.vo.CategoryTreeVo;
import com.github.caijh.mall.system.model.SystemRole;
import com.github.caijh.mall.system.request.SystemRoleListReqBody;
import com.github.caijh.mall.system.request.SystemRoleReqBody;
import com.github.caijh.mall.system.service.SystemRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/admin/system/role")
@Api(tags = "设置 -- 权限管理 -- 身份管理")
public class SystemRoleController extends BaseController {

    @Inject
    private SystemRoleService systemRoleService;

    /**
     * 分页显示身份管理表
     *
     * @param reqBody 搜索条件
     * @return page of SystemRole
     * @author Mr.Zhang
     * @since 2020-04-18
     */
    @ApiOperation(value = "分页列表")
    @PostMapping(value = "/list")
    public IPage<SystemRole> getList(@RequestBody @Validated SystemRoleListReqBody reqBody) {
        return systemRoleService.findPage(reqBody);
    }

    /**
     * 新增身份管理表
     *
     * @param systemRoleReqBody 新增参数
     * @author Mr.Zhang
     * @since 2020-04-18
     */
    @ApiOperation(value = "新增")
    @PostMapping(value = "/save")
    public void save(@RequestBody @Validated SystemRoleReqBody systemRoleReqBody) {
        SystemRole systemRole = new SystemRole();
        BeanUtils.copyProperties(systemRoleReqBody, systemRole);
        systemRoleService.save(systemRole);
    }

    /**
     * 删除身份管理表
     *
     * @param id Integer
     * @author Mr.Zhang
     * @since 2020-04-18
     */
    @ApiOperation(value = "删除")
    @GetMapping(value = "/delete")
    public void delete(@RequestParam(value = "id") Integer id) {
        systemRoleService.removeById(id);
    }

    /**
     * 修改身份管理表
     *
     * @param id                integer id
     * @param systemRoleReqBody 修改参数
     * @author Mr.Zhang
     * @since 2020-04-18
     */
    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public void update(@RequestParam Integer id, @RequestBody @Validated SystemRoleReqBody systemRoleReqBody) {
        systemRoleService.update(id, systemRoleReqBody);
    }

    /**
     * 查询身份管理表信息
     *
     * @param ids String
     * @return SystemRole list
     * @author Mr.Zhang
     * @since 2020-04-18
     */
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info")
    public List<SystemRole> info(@RequestParam(value = "ids") String ids) {
        return systemRoleService.list(Wrappers.<SystemRole>lambdaQuery().in(SystemRole::getId, Strings.toIntListByComma(ids)));
    }

    /**
     * 管理员菜单权限
     *
     * @return CategoryTreeVo list
     */
    @ApiOperation(value = "菜单")
    @GetMapping(value = "/menu")
    public List<CategoryTreeVo> menu() {
        return systemRoleService.menu();
    }

}



